<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "admin_user_role".
 *
 * @property int $id 主键
 * @property string $role_id 角色id
 * @property string $user_id 用户id
 * @property int $deleted 0 未删除 1已删除
 * @property string $created 创建时间
 * @property string $modified 修改时间
 */
class AdminUserRole extends BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin_user_role';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['role_id', 'user_id'], 'required'],
            [['deleted'], 'integer'],
            [['created', 'modified'], 'safe'],
            [['role_id'], 'string', 'max' => 50],
            [['user_id'], 'string', 'max' => 45],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', '主键'),
            'role_id' => Yii::t('app', '角色id'),
            'user_id' => Yii::t('app', '用户id'),
            'deleted' => Yii::t('app', '0 未删除 1已删除'),
            'created' => Yii::t('app', '创建时间'),
            'modified' => Yii::t('app', '修改时间'),
        ];
    }

    public function getRoleByUserId($userId)
    {
        $result = self::find()->andWhere(array('user_id' => $userId))->asArray()->all();
        return array_column($result, 'role_id');
    }
}
